Skip to content

fix(sse): stop reading at the DONE marker - #244

Merged
luk384090-cloud merged 1 commit into
dashscope:mainfrom
thzon:fix/sse-done-marker
Nov 25, 2026
Merged

luk384090-cloud merged 1 commit into
dashscope:mainfrom
thzon:fix/sse-done-marker

Conversation

@thzon

@thzon thzon commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

Description

The shared sync and async SSE streams currently skip [DONE] but continue reading the HTTP response until EOF. If a server leaves the connection open after this terminal event, consuming an otherwise completed stream can wait for more data or raise a later transport error.

Stop both iterators at [DONE]; their existing finally blocks close the response. Strengthen the sync regression test and add its async counterpart using a mock body that raises if another read is attempted after the terminal event. Both tests exercise the public chat completions API and assert that the response is closed.

Related Issue: Discovered through source inspection and reproduced locally; no existing issue.
Security Considerations: Tests use mock HTTP responses and a dummy API key. No live model requests or credentials are included.

Type of Change

  • Bug fix
  • New feature
  • Breaking change
  • Documentation
  • Refactoring

Component(s) Affected

  • Model
  • Application
  • Common
  • Documentation
  • Tests
  • CI/CD

Checklist

  • Pre-commit hooks pass
  • Tests pass locally (focused suite passes; two existing full-suite failures documented below)
  • Documentation updated (not needed for this bug fix)
  • Ready for review

Testing

Python 3.12.13:

  • Before the fix, python -m pytest tests/test_sse.py -k done_marker -q: both sync and async regression tests fail with read beyond the [DONE] marker.
  • After the fix, python -m pytest tests/test_sse.py tests/test_sse_comprehensive_verify.py tests/test_transport.py tests/test_response.py tests/test_response_drift.py -q: 250 passed.
  • pre-commit run --all-files: all applicable hooks passed.
  • python -m pytest tests --ignore=tests/legacy_1x -q (API-key environment variables unset): 5628 passed, 4 skipped, 2 failed. The failures are tests/cli/test_progress.py::TestSleepHook::test_mentions_the_job_id and tests/cli/test_progress.py::TestSleepHook::test_custom_label, which expect a progress label in stderr but receive an empty string. Both also fail when the SSE implementation is temporarily restored to the unchanged upstream version. The legacy suite was not run.
  • git diff --check: passed.

Additional Notes

AI assistance was used for source inspection, implementation, regression tests, and validation. The tests reproduce the issue using mock transport; this is not a claim of a live-service incident.

@luk384090-cloud

Copy link
Copy Markdown
Collaborator

在确认中

@lzsweb

lzsweb commented Nov 25, 2026

Copy link
Copy Markdown
Collaborator

Review

Verdict: LGTM — the fix is correct and the two full-suite failures are unrelated to this PR.

The 2 failing tests: environment-dependent, not introduced here

TestSleepHook::test_mentions_the_job_id and test_custom_label assert the label text that rich Status renders to stderr. The tty_stderr fixture fakes isatty()=True but does not control TERM. Under TERM=dumb, rich treats the console as a dumb terminal and Status renders nothing, so the captured stderr is empty.

Reproduced locally on this branch:

  • TERM=dumb python -m pytest tests/cli/test_progress.py -q → exactly these 2 fail, 23 pass (matches the report)
  • normal TERM, API-key env unset → 25 passed
  • CI is green on all legs (3.9–3.13)

Optional hardening (fine as a follow-up): make the fixture hermetic with monkeypatch.setenv("TERM", "xterm-256color") so the tests don't depend on the developer's terminal.

The SSE fix

  • Previously [DONE] was skipped by _next_event but the loop kept reading to EOF; a server holding the connection open could stall or raise a later transport error. Breaking at DONE_MARKER in both iterators with the existing finally closing the response matches the OpenAI SSE convention (and openai-python's behavior).
  • The regression tests are well designed: _OpenEndedSSEBody raises if read past the terminal event, and both assert response.is_closed.
  • Local run on the PR branch: tests/test_sse.py → 61 passed.

Only theoretical edge: a server emitting events after [DONE] would now be truncated — that violates the convention and is handled the same way by mainstream SDKs, so not a concern.

@luk384090-cloud
luk384090-cloud merged commit 7c8a0af into dashscope:main Nov 25, 2026
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants